//
//  ViewController.m
//  20503-下载图片练习
//
//  Created by student on 16/3/22.
//  Copyright © 2016年 wk. All rights reserved.
//

#import "ViewController.h"
#define edge 100
#define gap (([UIScreen mainScreen].bounds.size.width - 100*3)/4)

@interface ViewController ()

@property (nonatomic,strong) NSArray *arr;


@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.arr = @[@"http://www.apple.com.cn/mac-pro/images/static/processor.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/memory.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/graphics.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/storage.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/thermal.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/fan.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/expansion.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/wifi.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/design.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/comingsoon.jpg"];
    self.activityView.hidesWhenStopped = YES;
}

//serial
- (IBAction)pneumoniaBtnPressed:(id)sender {
    [self.activityView startAnimating];
    
            //创建子线程
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            dispatch_async(queue, ^{
                for (int i = 0; i <= self.arr.count/3; i++){
                    for (int j = 0; j < 3; j++) {
                        if ((3*i+j) > self.arr.count - 1) {
                            break;
                        }
                        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((j+1)*gap + j*edge, 100 + gap*(i+1)+i*edge, edge, edge)];
                        
                NSString *urlStr = self.arr[i*3+j];
                NSLog(@"%@",urlStr);
                NSURL *url = [NSURL URLWithString:urlStr];
                NSData *data = [NSData dataWithContentsOfURL:url];
                UIImage *image = [UIImage imageWithData:data];
                //返回主线程进行设置图片
                dispatch_async(dispatch_get_main_queue(), ^{
                    [imageView setImage:image];
                    [self.activityView stopAnimating];
                    [self.view addSubview:imageView];
                });
                    }
                }
            });

    
}

- (IBAction)serialBtnPressed:(id)sender {
    [self.activityView startAnimating];
        for (int i = 0; i <= self.arr.count/3; i++){
            for (int j = 0; j < 3; j++) {
                if ((3*i+j) > self.arr.count - 1) {
                    break;
                }
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((j+1)*gap + j*edge, 100 + gap*(i+1)+i*edge, edge, edge)];
    
    //创建子线程
    dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
    dispatch_async(queue, ^{
        
                NSString *urlStr = self.arr[i*3+j];
                NSLog(@"%@",urlStr);
                NSURL *url = [NSURL URLWithString:urlStr];
                NSData *data = [NSData dataWithContentsOfURL:url];
                UIImage *image = [UIImage imageWithData:data];
                //返回主线程进行设置图片
                dispatch_async(dispatch_get_main_queue(), ^{
                    [imageView setImage:image];
                    [self.activityView stopAnimating];
                    [self.view addSubview:imageView];
                });
       
        
    });
            }
        }
}



-(UIImage *)loadPicWithURL:(NSString *)str{
    NSURL *url = [NSURL URLWithString:str];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    return image;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
